const Base = require('../base.js');

module.exports = class extends Base {
  /**
   *
   * @api {GET} /orgusers/{id} 根据机构id获取该机构下的用户id，真实姓名及用户头像地址
   * @apiName 根据机构id获取用户信息接口
   * @apiGroup userinfo
   * @apiVersion  1.0.0
   *
   * @apiParam {Number} id Org unique ID
   *
   * @apiSuccess (200) {Object[]} user 用户信息
   *
   * @apiSuccess (200) {Number} user.id 用户id
   *
   * @apiSuccess (200) {String} user.name 用户姓名
   *
   * @apiSuccess (200) {String} user.pictureUrl 用户头像地址
   *
   * @apiParamExample  {type} Request-Example:
   * {
   *     "id": 13
   * }
   *
   *
   * @apiSuccessExample {json} Success-Response:
   * [{
   *         "id": 27,
   *         "realname": "杜文博",
   *         "pictureUrl": "userImages/TIM图片20171218101900.png"
   * }]
   *
   *
   */
  async getAction() {
    try {
      const orgId = this.get('id');
      const userorg = think.model('userorg');
      const user = think.model('user');
      const orgData = await userorg.where({ orgId }).field('userId').select();
      const userIdList = orgData.map((e) => {
        return `id = ${e.userId}`;
      });
      if (!userIdList.length) {
        this.body = [];
        return;
      }
      const userData = await user.where(userIdList.join(' OR ')).field('id, realname, pictureid').select();
      const { getUserImg } = await think.service('userinfo/user');
      const userDataWithImgUrl = await getUserImg(userData);
      this.body = userDataWithImgUrl;
    } catch (error) {
      this.fail('502');
      think.logger.debug(error);
    }
  }
};
